package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Log
    //1、新增部门
    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept) {
        log.info("新增部门：{}", dept);
        deptService.addDept(dept);
        return Result.success();

    }

    //2、条件分页查询部门
    @GetMapping("/depts")
    public Result listDepts(String name, Integer status,
    @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
//System.out.println("查询部门：" + name + " " + status + " " + page + " " + pageSize);
    log.info("分页查询部门：name={},status={},page={},pageSize={}", name, status, page, pageSize);
    PageResult<Dept> pageResult = deptService.selectlist(name, status, page, pageSize);
        return Result.success(pageResult);
    }

    //3、根据id查询部门
    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id) {
        //System.out.println("查询部门：" + id);
        log.info("查询部门：{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @Log
    //4、修改部门
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept) {
        //System.out.println("修改部门：" + dept);
        log.info("修改部门：{}", dept);
        deptService.update(dept);
        return Result.success();
    }
    @Log
    //5、根据id删除部门
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门：{}", id);
        deptService.delete(id);
        return Result.success();
    }

    //查询所有部门数据
    @GetMapping("/depts/list")
    public Result list(){
        List<Dept> list = deptService.list();
        return Result.success(list);
    }





}
